<?php
include ("includes/header.php");
include ("includes/connection.php");
include ("includes/login.php");
include ("includes/iniciaSessio.php");
?>

<!-- No hace falta JAVI-->
<!--Script que tria l'opció del select.sexe-->
<script type="text/javascript">
	function selecOp() {
		var op = document.getElementById("select");
		var tt = document.getElementById("sexe");
		if(op.selectedIndex == 0)
			tt.value = "";
		if(op.selectedIndex == 1)
			tt.value = "Home";
		if(op.selectedIndex == 2)
			tt.value = "Dona";
	}
</script>
<body>
	<?php
$alias=$_SESSION["username"];
$sql="SELECT * FROM usuari where alias ='$alias'";
$res=mysql_query($sql);
if (!$res) {
$missatge = 'Consulta Invalida: ' . mysql_error() . "\n";
$missatge .= 'Consulta SQL: ' . $sql;
}
if ($row=mysql_fetch_array($res))
	?>
	<?php
	include ("includes/contentHeader.php");
	?>
	<!--contingut 1-->
	<section class="container">
		<!--fons gris-->
		<div class="hero-unit">
			<form name="registre" action="perfil_modificar.php"  class="form-horizontal" method="post">
				<div class="row-fluid show-grid">
					<fieldset class="span6 ">
						<legend>
							Modificar Registre de Usuari
						</legend>
						<label  for="nom">Nom:</label>
						<input class="input-medium" name="nom" type="text" value="<?php echo $row['nomUsuari']?>"/>
						<label  for="cognom1">Primer Cognom:</label>
						<input class="input-medium" name="cognom1" type="text" value="<?php echo $row['cognom1']?>"/>
						<label  for="cognom2">Segon Cognom:</label>
						<input class="input-medium" name="cognom2" type="text" value="<?php echo $row['cognom2']?>"/>
						<label  for="email">email:</label>
						<input class="input-medium" name="email" type="text" value="<?php echo $row['email']?>"/>
						<label  for="seexe">Sexe:</label>
						<!--<input class="input-medium" name="sexe" id="sexe" type="text" value="<?php echo $row['sexe']?>" disabled="false"/>-->
						<select name="sexe" id="sexe"  class="input-medium "/>
						<!--onchange="selecOp()"-->
						<option value="<?php echo $row['sexe']?>"><?php echo $row['sexe']
							?></option>
						<option value="home">Home</option>
						<option value="dona">Dona</option>
						</select> <label  for="data">Data de naixement:</label>
						<input type="text"  name="data" class="input-medium" id="dp1" value="<?php echo $row['dataNaixement']?>" >
						<label  for="biografia">Biografia: (Opcional)</label>
						<?php
						if (!isset($row['biografia'])) {
							echo '<textarea rows="5" cols="50" name="biografia"></textarea>';
							echo '</br></br>';
						} else
							echo '<textarea rows="5" cols="50" name="biografia">' . $row['biografia'] . '</textarea>';
						?><br>
						<br>
						<button class="btn" name="modificarperfil" type="submit" value="Modificar" >
							Modificar
						</button>
						<button class="btn" name="insert" type="button" value="Cancelar" onmousedown="location.href = 'perfil_usuari.php'";>
							Cancelar
						</button>
			</form>
			<?php
			if (isset($_POST['modificarperfil'])) {
				if (isset($_SESSION["sessionID"])) {
					function conveteixData($data) {
						$dataC = explode("-", $data);
						return $dataC[2] . "-" . $dataC[1] . "-" . $dataC[0];
						///print_r($dataC);
					}

					$email = $_POST['email'];
					$nom = $_POST['nom'];
					$cog1 = $_POST['cognom1'];
					$cog2 = $_POST['cognom2'];
					$sexe = $_POST['sexe'];
					$data = $_POST['data'];
					$dataCon = conveteixData($data);
					$biografia = $_POST['biografia'];
					$user = $_SESSION["username"];

					$consulta = "UPDATE usuari SET nomUsuari = '$nom', cognom1 = '$cog1', cognom2 = '$cog2', sexe = '$sexe', dataNaixement='$dataCon',biografia='$biografia' WHERE alias = '$user'";

					$result = (mysql_query($consulta, $conexion));

					if ($result) {
						echo "	<script>
alert('Perfil modificat.');
</script>";
						echo "<META HTTP-EQUIV='refresh' CONTENT='0'; URL='perfil_modificar.php'>";
					} else {
						echo "	<script>
alert('Error al modificar perfil.');
</script>";
					}

				}
			}
			?>
			</fieldset>
			<div class="span6">
				<img src="images/registro2.png" width=300 height=300 border=2 alt="Comparteix coche">
				<br />
			</div>
		</div>
		</div> <!-- fi fons gris-->
	</section>
	<!-- fi contingut 1-->
	<!--contingut 2-->
	<section class="container">
		<div class="hero-unit"></div>
	</section>
	<!-- fi contingut 2-->
	<hr>
	<?php
	include ("includes/footer.php");
	?>
